package com.byjh.api;


import com.byjh.app.domain.ExamLogs;
import com.byjh.app.domain.ExamPaper;
import com.byjh.app.domain.Question;
import com.byjh.app.domain.Recruit;
import com.byjh.app.dto.RecruitSearchDto;
import com.byjh.app.dto.SubExamDto;
import com.byjh.app.service.IExamLogsService;
import com.byjh.app.service.IExamPaperService;
import com.byjh.app.service.IQuestionService;
import com.byjh.app.service.IRecruitService;
import com.byjh.common.annotation.Anonymous;
import com.byjh.common.core.controller.BaseController;
import com.byjh.common.core.domain.R;
import org.apache.ibatis.cursor.Cursor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/api/exam")
public class ExamApi extends BaseController {

    // 考试题目数量
    private static final int examSize = 100;

    @Resource
    private IQuestionService questionService;
    @Resource
    private IExamPaperService examPaperService;

    @Resource
    private IExamLogsService examLogsService;




    /**
     * 生成考试试卷
     *
     * @return
     */
    @GetMapping("/create")
    public R<List<Question>> create() {
        return R.ok(questionService.generateExamPaper(examSize));
    }

    /**
     * 提交考试
     *
     * @return
     */
    @PostMapping("/subExam")
    public R<Long> subExam(@RequestBody SubExamDto dto) {
        Long id = examPaperService.insertExamPaper(new ExamPaper()
                .setQuestionNumber(dto.getQuestionNumber())
                .setRightNumber(dto.getRightNumber())
                .setErrorNumber(dto.getErrorNumber())
                .setTime(dto.getTime())
                .setScore(dto.getScore()));

        // 批量插入记录
        examLogsService.batchInsertExamLogs(dto.getLogList(),id);
        return R.ok(id);
    }

    /**
     * 获取考试试卷
     *
     * @param id
     * @return
     */
    @GetMapping("/result/{id}")

    public R<ExamPaper> result(@PathVariable("id") Long id) {
        ExamPaper examPaper = examPaperService.selectExamPaperById(id);
        // 获取考试记录
        examPaper.setLogsList(examLogsService.selectExamLogsList(new ExamLogs().setExamId(id)));
        return R.ok(examPaper);
    }
}
